/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2017
 */

package com.database.management.web;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.database.management.base.BaseDao;
import com.database.management.common.Result;
import com.database.management.domain.DbConnection;
import com.database.management.service.IDbConnectionService;

@Controller
@RequestMapping("/dbConnection")
public class DbConnectionController {
	private Logger log = LoggerFactory.getLogger(DbConnectionController.class);
	@Autowired
	private IDbConnectionService iDbConnectionService;
	
	@ResponseBody
	@RequestMapping(path="/save",method=RequestMethod.POST)
	public Result save(DbConnection dbConnection) {
		Result result = new Result();
		if(dbConnection!=null){
			dbConnection.setCreatedate(new Date());
			DbConnection db = iDbConnectionService.insert(dbConnection);
			if (db.getId()  != null) {
				BaseDao.addConnection(db);
				result.setSuccess("保存成功");
			} else {
				result.setError("保存失败");
			}
		}else{
			result.setError("前台传递参数错误！");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(path="/update",method=RequestMethod.POST)
	public Result update(DbConnection dbConnection) {
		Result result = new Result();
		if(dbConnection!=null && dbConnection.getId() != null){
			DbConnection db = iDbConnectionService.update(dbConnection);
			if (db.getId()  != null) {
				result.setSuccess("更新成功");
			} else {
				result.setError("更新失败");
			}
		}else{
			result.setError("前台传递参数错误！");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(path="/delete",method=RequestMethod.POST)
	public Result delete(Integer id) {
		Result result = new Result();
		if(id!=null ){
			iDbConnectionService.delete(id);
				result.setSuccess("删除成功");
		}else{
			result.setError("前台传递参数错误！");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/findAll")
	public List<DbConnection> findAll(DbConnection dbConnection) {
		return iDbConnectionService.findAll(Example.of(dbConnection),new Sort("sort"));
	}
	
	@ResponseBody
	@RequestMapping("/findPage")
	public Result findPage(DbConnection dbConnection,Integer page, @RequestParam(defaultValue = "20")Integer size) {
		Result result = new Result();
		if(dbConnection!=null ){
			result.setSuccess("查询成功");
			PageRequest pageable =  new PageRequest(page, size);
			result.setResult(iDbConnectionService.findPage(Example.of(dbConnection),pageable));
		}else{
			result.setError("前台传递参数错误！");
		}
		return result;
	}

	
}

